﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace HYTools
{
	public partial class UpgradeFileForm : System.Windows.Forms.Form
	{
		public UpgradeFileForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
		}

		private void UpgradeFileForm_Load(object sender, EventArgs e)
		{
			this.LoadCompornentData();
		}

		private void UpgradeFileForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveCompornentData();
		}

		private void OnStop()
		{
			this.labelCurrentUpdate.Text = "等待升级";
			this.progressBar.Value = 0;
		}

		private void btnStart_Click(object sender, EventArgs e)
		{
			if (!this.chbRVT.Checked && !this.chkRFA.Checked && !this.chkRTE.Checked && !this.chkRFT.Checked)
			{
				YJKMessageBox.Show("没有选中文件类型！");
				return;
			}
			if (this.tbSPath.Text.CompareTo("") == 0)
			{
				YJKMessageBox.Show("源路径不能为空！");
				this.OnStop();
				return;
			}
			if (this.tbDPath.Text.CompareTo("") == 0)
			{
				YJKMessageBox.Show("目标路径不能为空！");
				this.OnStop();
				return;
			}
			if (this.tbSPath.Text.CompareTo(this.tbDPath.Text) == 0)
			{
				YJKMessageBox.Show("源路径不能与目标路径相同！");
				this.OnStop();
				return;
			}
			this.tbOutput.Text = "";
			List<Document> list = new List<Document>();
			List<string> list2 = new List<string>();
			string text = this.SourcePath();
			if (!Directory.Exists(text))
			{
				YJKMessageBox.Show("源路径不存在，请确认！");
				this.OnStop();
				return;
			}
			List<string> allFile = this.GetAllFile(text);
			if (allFile.Count == 0)
			{
				this.tbOutput.Text = "没有需要升级的文件!";
				return;
			}
			this.progressBar.Maximum = allFile.Count;
			this.progressBar.Value = 0;
			string text2 = "";
			for (int i = 0; i < allFile.Count; i++)
			{
				try
				{
					string item = "";
					this.UpgradeFile(i + 1, allFile[i], ref item, ref list, ref text2);
					list2.Add(item);
					this.progressBar.Value++;
					this.Refresh();
				}
				catch (Exception ex)
				{
					ex.ToString();
					this.progressBar.Value++;
					this.Refresh();
				}
			}
			text2 += "文件升级完毕。";
			this.tbOutput.Text = text2;
			this.OnStop();
		}

		private void btnSPath_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			string text = folderBrowserDialog.SelectedPath.ToString();
			if (text.CompareTo(this.tbDPath.Text) == 0)
			{
				YJKMessageBox.Show("源路径不能与目标路径相同！");
				return;
			}
			this.tbSPath.Text = text;
		}

		private void btnDPath_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			if (folderBrowserDialog.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			string text = folderBrowserDialog.SelectedPath.ToString();
			if (text.CompareTo(this.tbSPath.Text) == 0)
			{
				YJKMessageBox.Show("源路径不能与目标路径相同！");
				return;
			}
			this.tbDPath.Text = text;
		}

		private void tbSPath_Validating(object sender, CancelEventArgs e)
		{
			if (this.tbSPath.Text.CompareTo("") == 0)
			{
				return;
			}
			if (Path.HasExtension(this.tbSPath.Text))
			{
				YJKMessageBox.Show("源路径无效.");
				e.Cancel = true;
				this.tbSPath.Text = "";
				return;
			}
		}

		private void tbDPath_Validating(object sender, CancelEventArgs e)
		{
			if (this.tbDPath.Text.CompareTo("") == 0)
			{
				return;
			}
			if (Path.HasExtension(this.tbDPath.Text))
			{
				YJKMessageBox.Show("目标路径无效.");
				e.Cancel = true;
				this.tbDPath.Text = "";
				return;
			}
		}

		private List<string> GetAllFile(string directory)
		{
			List<string> list = new List<string>();
			DirectoryInfo directoryInfo = new DirectoryInfo(directory);
			try
			{
				if (this.chbRVT.Checked)
				{
					foreach (FileInfo fileInfo in directoryInfo.GetFiles("*.RVT"))
					{
						list.Add(fileInfo.FullName);
					}
				}
				if (this.chkRFA.Checked)
				{
					foreach (FileInfo fileInfo2 in directoryInfo.GetFiles("*.RFA"))
					{
						list.Add(fileInfo2.FullName);
					}
				}
				if (this.chkRTE.Checked)
				{
					foreach (FileInfo fileInfo3 in directoryInfo.GetFiles("*.RTE"))
					{
						list.Add(fileInfo3.FullName);
					}
				}
				if (this.chkRFT.Checked)
				{
					foreach (FileInfo fileInfo4 in directoryInfo.GetFiles("*.RFT"))
					{
						list.Add(fileInfo4.FullName);
					}
				}
				foreach (DirectoryInfo directoryInfo2 in directoryInfo.GetDirectories("*"))
				{
					list.AddRange(this.GetAllFile(directoryInfo2.FullName));
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message.ToString(), "提示");
			}
			return list;
		}

		[DllImport("kernel32.dll")]
		public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

		[DllImport("kernel32.dll")]
		public static extern bool CloseHandle(IntPtr hObject);

		private void UpgradeFile(int index, string sFileFillPath, ref string dFileFillPath, ref List<Document> docList, ref string outputString)
		{
			this.labelCurrentUpdate.Text = "正在升级文件:" + sFileFillPath;
			new SaveAsOptions().OverwriteExistingFile = true;
			string text = this.SourcePath();
			string text2 = sFileFillPath.Substring(text.Length, sFileFillPath.Length - text.Length);
			dFileFillPath = this.DestinationPath() + text2;
			string str = text2.Substring(text2.Length - 4, 4);
			string path = this.GetPath(dFileFillPath);
			if (!Directory.Exists(path))
			{
				Directory.CreateDirectory(path);
			}
			string str2 = "temp" + index.ToString() + str;
			Path.GetTempPath() + str2;
			try
			{
				File.Copy(sFileFillPath, dFileFillPath, true);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				outputString = outputString + this.GetFileName(sFileFillPath) + "升级失败\r\n";
				return;
			}
			Document document = this.m_Revit.Application.Application.OpenDocumentFile(dFileFillPath);
			if (document == null)
			{
				outputString = outputString + this.GetFileName(sFileFillPath) + "升级失败\r\n";
				return;
			}
			docList.Add(document);
			outputString = outputString + this.GetFileName(sFileFillPath) + "升级成功\r\n";
			try
			{
				RevitVersionFuncs.Save(document);
				document.Close();
				this.tbOutput.Text = outputString;
				try
				{
					string text3 = dFileFillPath;
					string str3 = text3.Substring(0, text3.LastIndexOf("."));
					File.Delete(str3 + ".0001." + dFileFillPath.Split(new char[]
					{
						'.'
					})[dFileFillPath.Split(new char[]
					{
						'.'
					}).Length - 1]);
					File.Delete(str3 + ".0002." + dFileFillPath.Split(new char[]
					{
						'.'
					})[dFileFillPath.Split(new char[]
					{
						'.'
					}).Length - 1]);
				}
				catch
				{
				}
			}
			catch (Exception ex2)
			{
				string message2 = ex2.Message;
			}
		}

		private string SourcePath()
		{
			string text = this.tbSPath.Text;
			int num = text.Length - 1;
			if (text.LastIndexOf("\\") != num || text.LastIndexOf("/") != num)
			{
				text += "\\";
			}
			return text;
		}

		private string DestinationPath()
		{
			string text = this.tbDPath.Text;
			int num = text.Length - 1;
			if (text.LastIndexOf("\\") != num || text.LastIndexOf("/") != num)
			{
				text += "\\";
			}
			return text;
		}

		private string GetPath(string fileFillPath)
		{
			int num = fileFillPath.LastIndexOf("\\");
			return fileFillPath.Substring(0, num + 1);
		}

		private string GetFileName(string fileFillPath)
		{
			int num = fileFillPath.LastIndexOf("\\");
			return fileFillPath.Substring(num + 1, fileFillPath.Length - num - 1);
		}

		private void LoadCompornentData()
		{
			this.tbOutput.Text = "";
			this.progressBar.Value = 0;
			this.labelCurrentUpdate.Text = "等待升级";
			this.OnStop();
			string path = Path.GetTempPath() + "UpgradeFileForm.dat";
			if (!File.Exists(path))
			{
				this.tbSPath.Text = "";
				this.tbDPath.Text = "";
				this.chbRVT.Checked = true;
				this.chkRFA.Checked = true;
				this.chkRTE.Checked = true;
				this.chkRFT.Checked = true;
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			this.tbSPath.Text = streamReader.ReadLine();
			this.tbDPath.Text = streamReader.ReadLine();
			this.chbRVT.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkRFA.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkRTE.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.chkRFT.Checked = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveCompornentData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "UpgradeFileForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.tbSPath.Text);
			streamWriter.WriteLine(this.tbDPath.Text);
			streamWriter.WriteLine(this.chbRVT.Checked);
			streamWriter.WriteLine(this.chkRFA.Checked);
			streamWriter.WriteLine(this.chkRTE.Checked);
			streamWriter.WriteLine(this.chkRFT.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private ExternalCommandData m_Revit;
	}
}
